<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
		<uni-forms-item name="title" label="专辑">
		  <uni-easyinput v-model="parentData.title" :disabled="true"></uni-easyinput>
		</uni-forms-item>
      <uni-forms-item name="title" label="标题">
        <uni-easyinput v-model="formData.title"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="sort" label="排序">
        <uni-easyinput type="number" v-model="formData.sort"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="is_publish" label="是否发布">
        <switch @change="binddata('is_publish', $event.detail.value)" :checked="formData.is_publish"></switch>
      </uni-forms-item>
     
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/timbre_title.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'timbre_title';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "title": "",
        "type": "title",
        "sort": 0,
        "parent_id": null,
        "is_publish": false,
      }
      return {
        formData,
		parentData:{},//父类数据
        formOptions: {
          "type_localdata": [
            {
              "value": "title",
              "text": "title"
            },
            {
              "value": "chapter",
              "text": "chapter"
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
	onLoad(e) {
		if(e && e.parent_id){
			this.formData.parent_id = e.parent_id;
			this.formData.type = 'chapter';
			//查询出对应的父类标题
			db.collection('timbre_title').where({_id:e.parent_id}).field('_id,title').get({getOne:true}).then(res=>{
				this.parentData = res.result.data;
			});
		}
		
	},
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
		  value.parent_id = this.formData.parent_id;
		  value.type = this.formData.type;
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
